package com.finace.money.ui.activity;

import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.util.DrawableUtil;
import com.finace.money.common.util.RxCountDown;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.ImageBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.di.component.DaggerOpenAccountComponent;
import com.finace.money.di.module.OpenAccountModule;
import com.finace.money.di.module.VerifyCodeModule;
import com.finace.money.net.rto_rxbuild.RxBus;
import com.finace.money.presenter.OpenAccountPresenter;
import com.finace.money.presenter.VerifyCodePresenter;
import com.finace.money.presenter.contract.OpenAccountContract;
import com.finace.money.presenter.contract.VerifyCodeContract;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by zhangpeng on 2018/1/26.
 */

public class OpenAccountActivity extends BaseActivity<OpenAccountPresenter> implements View.OnClickListener,
        OpenAccountContract.View, VerifyCodeContract.View {
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;
    @BindView(R.id.et_phone_number)
    EditText etPhoneNumber;
    @BindView(R.id.et_verify_code)
    EditText etVerifyCode;
    @BindView(R.id.button_get_verify_code)
    Button buttonGetVerifyCode;
    @BindView(R.id.button_next)
    Button buttonNext;
    @BindView(R.id.et_image_code)
    EditText etImageCode;
    @BindView(R.id.iv_image_code)
    ImageView ivImageCode;
    private Disposable disposable;
    private RxCountDown rxCountDown;

    @Inject
    public VerifyCodePresenter mVerifyCodePresenter;

    @Override
    public int setLayout() {
        return R.layout.activity_verify_phone;
    }

    @Override
    public void init() {
        tvHeadTitle.setText(UIUtil.getString(R.string.open_account));
        rlLeft.setOnClickListener(this);
        buttonGetVerifyCode.setOnClickListener(this);
        buttonNext.setOnClickListener(this);
        ivImageCode.setOnClickListener(this);
        mVerifyCodePresenter.getImageCode();
        disposable = RxBus.getDefault().toObservable(Message.class).subscribe(new Consumer<Message>() {
            @Override
            public void accept(Message message) throws Exception {
                if (message.arg2 == 2) {
                    buttonGetVerifyCode.setBackgroundResource(R.drawable.back_verify_code);
                    buttonGetVerifyCode.setClickable(true);
                    buttonGetVerifyCode.setText(UIUtil.getString(R.string.send_again));
                }
            }
        });
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

        DaggerOpenAccountComponent.builder().appComponent(appComponent)
                .openAccountModule(new OpenAccountModule(this))
                .verifyCodeModule(new VerifyCodeModule(this))
                .build().inject(this);
    }

    @Override
    public void onClick(View view) {
        String phone = etPhoneNumber.getText().toString().trim();
        switch (view.getId()) {
            case R.id.rl_left:
                onBackPressed();
                break;
            case R.id.iv_image_code:
                mVerifyCodePresenter.getImageCode();
                break;
            case R.id.button_get_verify_code:
                if (TextUtils.isEmpty(phone)) {
                    UIUtil.showLongToastSafe(getString(R.string.phone_number_input));
                    return;
                }
                mVerifyCodePresenter.sendCode(phone, "2");
                buttonGetVerifyCode.setClickable(false);
                break;
            case R.id.button_next:
                if (TextUtils.isEmpty(phone)) {
                    UIUtil.showLongToastSafe(getString(R.string.phone_number_input));
                    return;
                }
                String verifyCode = etVerifyCode.getText().toString().trim();
                if (TextUtils.isEmpty(verifyCode)) {
                    UIUtil.showLongToastSafe(getString(R.string.verify_code_input));
                    return;
                }
                String imageCode = etImageCode.getText().toString().trim();
                if (TextUtils.isEmpty(verifyCode)) {
                    UIUtil.showLongToastSafe(getString(R.string.image_code_input));
                    return;
                }
                mPresenter.checkPhone(phone,imageCode, verifyCode);

                break;
        }
    }

    @Override
    public void checkSucess() {
        if (null != rxCountDown) {
            rxCountDown.stop();
        }
        Intent intent = new Intent(UIUtil.getContext(), TextInfoActivity.class);
        intent.putExtra("phone",etPhoneNumber.getText().toString().trim());
        intent.putExtra("code",etVerifyCode.getText().toString().trim());
        intent.putExtra("imgcode",etImageCode.getText().toString().trim());
        startActivity(intent);
        finish();
    }

    @Override
    public void sendSucceed() {
        buttonGetVerifyCode.setBackgroundResource(R.drawable.back_verify_code_unenable);
        UIUtil.showToastSafe(getString(R.string.verify_code_send));
        rxCountDown = new RxCountDown();
        rxCountDown.startCountDown(60, new RxCountDown.ShowCountDown() {
            @Override
            public void show(int i) {
                buttonGetVerifyCode.setText(i + "S后获取");
                if (i == 0) {
                    Message message = new Message();
                    message.arg1 = i;
                    message.arg2 = 2;
                    RxBus.getDefault().post(message);
                }
            }
        });
    }

    @Override
    public void getImageSucess(ImageBean imageBean) {

        ivImageCode.setImageBitmap(DrawableUtil.stringtoBitmap(imageBean.getImg_code_url()));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disposable.dispose();
        if (null != rxCountDown) {
            rxCountDown.stop();
        }
    }

    @Override
    public void showError(String msg) {
        super.showError(msg);
        if (null != buttonGetVerifyCode) {
            buttonGetVerifyCode.setClickable(true);
            buttonGetVerifyCode.setBackgroundResource(R.drawable.back_verify_code);
        }
    }

}
